package service

import (
	"fmt"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/model"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/app/admin/internal/vo"
	"github.com/vueadmin/utils/conv"
)

type carService struct{
	svcCtx *svc.ServiceContext
}

func NewCarService(svcCtx *svc.ServiceContext) *carService {
	return &carService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p carService) GetPageList(req *dto.CarPageReq) ([]*vo.CarList, int64, error) {
	var (
		entity model.Car
		list  []*vo.CarList
		count int64
	)
	query := p.svcCtx.Db.Model(&entity)
	if !conv.IsEmpty(req.CarId) {
		query = query.Where("car_id = ?", req.CarId)
	}
	if !conv.IsEmpty(req.MembeId) {
		query = query.Where("membe_id = ?", req.MembeId)
	}
	orderBy := "car_id desc"
	if !conv.IsEmpty(req.Sort) && !conv.IsEmpty(req.Order) {
		orderBy = fmt.Sprintf("%s %s", req.Sort, req.Order)
	}
	query.Count(&count)
	if err := query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit).Order(orderBy).Find(&list).Error; err != nil {
		return nil, 0, err
	}
	return list, count, nil
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func (p carService) UpdateExt(req map[string]interface{}) error {
	entity := model.Car{}
	if err := p.svcCtx.Db.Model(&entity).Where("car_id",conv.Int(req["car_id"])).Updates(req).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func (p carService) Add(req *dto.CarAddReq) (int, error) {
	entity := model.Car{}
	entity.MembeId = req.MembeId
	entity.CarNo = req.CarNo
	entity.CreateTime = conv.NowUnix()

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	return entity.CarId, nil
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func (p carService) Update(req *dto.CarUpdateReq) error {
	entity := model.Car{
		CarId: req.CarId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.MembeId = req.MembeId
	entity.CarNo = req.CarNo
	entity.CreateTime = conv.UnixTime(req.CreateTime)

	field := conv.StringToSlice("membe_id,car_no,create_time", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func (p carService) GetUpdateInfo(req *dto.CarGetUpdateInfoReq) (*model.Car, error) {
	entity := model.Car{}
	if err := p.svcCtx.Db.Where("car_id = ?",req.CarId).Limit(1).First(&entity).Error; err != nil {
		return nil, err
	}
	return &entity, nil
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func (p carService) Delete(req *dto.CarDeleteReq) error {
	query := p.svcCtx.Db
	query = query.Where("car_id IN ?", req.CarId)
	if err := query.Delete(&model.Car{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p carService) Detail(req *dto.CarDetailReq) (*vo.CarDetail, error) {
	var info vo.CarDetail
	query := p.svcCtx.Db.Model(&model.Car{})
	if err := query.Where("car_id = ?",req.CarId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

